package com.hliushi.learn.utils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import static java.math.BigDecimal.ROUND_HALF_DOWN;

/**
 * @author llhuang10
 * @date 2021/7/23 20:32
 */
public class RandomUtil {


    private static Random random = new Random();
    /**
     * 随机数组
     */
    private static String[] dateQualityRatingList = new String[]{"A", "B", "C", "D"};


    public static String randQualityRating() {
        return dateQualityRatingList[random.nextInt(dateQualityRatingList.length)];
    }

    public static void main(String[] args) {

        for (int i = 0; i < 10; i++) {
            List<Double> doubles = randArr(5);
            System.out.println(doubles);
        }
        for (int i = 0; i < 10; i++) {

            Double result = test();
            System.out.println(result);
        }
        // for (int i = 0; i < 10; i++) {
        //     String date = dateQualityRatingList[random.nextInt(dateQualityRatingList.length)];
        //     System.out.println(date);
        // }
        System.out.println("--------------------------------------------");

        for (int i = 0; i < 10; i++) {

            int intValue = RandomUtil.customNum(0, 0, 10).intValue();
            System.out.println(intValue);
        }
    }

    /**
     * @param
     * @return
     */
    public static List<Double> randArr(int length) {
        int nextInt = random.nextInt(length);
        if (nextInt == 0) {
            nextInt = 1;
        }
        List<Double> result = new ArrayList<>();

        for (int i = 0; i < nextInt; i++) {
            int num = random.nextInt(5) + 1;

            result.add((double) num);
        }

        return result;

    }

    /**
     * 思路 先保证有最小值;
     * 最大与最小的差值;
     * 差值 * (0–1) + 最小值 就是要的随机数;
     * 最后保留指定位数的小数,得到最终结果;
     *
     * @return
     */
    public static Double test() {
        int precimal = 4;//保留的小数位数
        double min = 0;//最小值
        double max = 1;//最大
        double value = random.nextDouble() * (max - min) + min;
        String result = BigDecimal.valueOf(value).setScale(precimal, ROUND_HALF_DOWN).toString();

        return Double.parseDouble(result);
    }


    /**
     * 自定义生成带有小数位置的数
     *
     * @param precimal 保留的小数位数
     * @param min      最小值
     * @param max      最大
     * @return 随机数
     */
    public static Double customNum(int precimal, double min, double max) {
        double value = random.nextDouble() * (max - min) + min;
        return BigDecimal.valueOf(value).setScale(precimal, ROUND_HALF_DOWN).doubleValue();
    }


}
